package Doan;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

class SQLServer implements Runnable {
	private Socket sock;
	private BufferedInputStream bis;
	private DataInputStream in;
	private PrintStream out;
	String line1, line2, line3, line4;

	public SQLServer(Socket s) {
		sock = s;
	}

	public void run() {
		try {
			bis = new BufferedInputStream(sock.getInputStream());
			in = new DataInputStream(bis);
			out = new PrintStream(sock.getOutputStream());
			while (true) {
				line1 = in.readLine();// nhan du lieu tu client
				// out.flush();
				line2 = in.readLine();
				// out.flush();
				line3 = in.readLine();
				// out.flush();
				line4 = in.readLine();
				if (line1.startsWith("DangNhap")) {
					Dangnhap(line2, line3);

				}

				else if (line1.startsWith("XemSoDu"))
					Xemsodu(line2);
				else if (line1.startsWith("NapTien")) {
					int sotien = Integer.parseInt(line3);
					Nap(line2, sotien);
				} else if (line1.startsWith("RutTien")) {
					int sotien = Integer.parseInt(line3);
					Rut(line2, sotien);
				} else if (line1.startsWith("ChuyenKhoan")) {
					int sotien = Integer.parseInt(line4);
					Chuyenkhoan(line2, line3, sotien);
				} else if (line1.startsWith("DoiMatKhau"))
					Doimatkhau(line2, line3);
				else if (line1.startsWith("KiemTra"))
					Kiemtra(line2);
			}

		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public String Getaccount(String account) {
		return account;

	}

	// Dang nhap tu client
	public void Dangnhap(String stk, String mk) throws Exception {
		int sodong = 0;
		String hoten = null;
		String url = "com.mysql.jdbc.Driver";
		String sql = "select* from khachhang where sotaikhoan='" + stk
				+ "' and matkhau='" + mk + "'";
		try {
			Class.forName(url);
			Connection cn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/atm", "root", "");
			PreparedStatement stmt = cn.prepareStatement(sql);
			ResultSet r = stmt.executeQuery();// thuc thi
			while (r.next()) {
				String sotk = r.getString("sotaikhoan");
				String makhau = r.getString("matkhau");
				hoten = r.getString("hoten");
				int sotien = r.getInt("tongtien");
				String dc = r.getString("diachi");
				String sodt = r.getString("sodienthoai");
				System.out.println(stk + "\t" + mk + "\t" + hoten + "\t"
						+ sotien + "\t" + dc + "\t" + sodt);
				sodong++;
			}
			cn.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		out.println(sodong);
		out.flush();
		out.println(hoten);
		out.flush();
	}

	// Xem so du tu client
	public void Xemsodu(String stk) throws Exception {
		int sotien = 0;
		String url = "com.mysql.jdbc.Driver";
		synchronized (Getaccount(stk)) {

			try {
				Class.forName(url);
				Connection cn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/atm", "root", "");
				String sql = "select* from khachhang where sotaikhoan='" + stk
						+ "'";
				PreparedStatement stmt = cn.prepareStatement(sql);
				ResultSet r = stmt.executeQuery();// thuc thi
				while (r.next()) {
					sotien = r.getInt("tongtien");
				}
				cn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			out.println(sotien);
			out.flush();
		}
	}

	// Yeu cau nap tien tu client
	public void Nap(String stk, int sotien) throws Exception {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		String day = sdf.format(cal.getTime());
		int sodong = 0;
		int tongtien = 0;
		String url = "com.mysql.jdbc.Driver";
		synchronized (Getaccount(stk)) {

			try {
				Class.forName(url);
				Connection cn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/atm", "root", "");
				String sql = "select* from khachhang where sotaikhoan='" + stk
						+ "'";
				PreparedStatement stmt = cn.prepareStatement(sql);
				ResultSet r = stmt.executeQuery();// thuc thi lay ra tong tien
				while (r.next()) {
					tongtien = r.getInt("tongtien");
				}
				tongtien = tongtien + sotien;
				String sql1 = "update khachhang set tongtien=" + tongtien
						+ " where sotaikhoan='" + stk + "'";
				PreparedStatement stmt1 = cn.prepareStatement(sql1);
				sodong = stmt1.executeUpdate();
				String sql2 = "insert into lichsugiaodich(sotaikhoan,ngaythang,sotien,lydo) Values('"
						+ stk + "'," + day + "," + sotien + ",'NapTien')";
				PreparedStatement stmt2 = cn.prepareStatement(sql2);
				stmt2.executeUpdate();
				System.out.println("Da ghi lai lich su giao dich");
				cn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			out.println(sodong);
			out.flush();
			out.println(tongtien);
			out.flush();
		}
	}

	// Yeu cau tut tien tu client
	public void Rut(String stk, int sotien) throws Exception {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy");
		String day = sdf.format(cal.getTime());
		int sodong = 0;
		int tongtien = 0;
		String url = "com.mysql.jdbc.Driver";
		synchronized (Getaccount(stk)) {

			try {
				Class.forName(url);
				Connection cn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/atm", "root", "");
				String sql = "select* from khachhang where sotaikhoan='" + stk
						+ "'";
				PreparedStatement stmt = cn.prepareStatement(sql);
				ResultSet r = stmt.executeQuery();// thuc thi lay tong tien
				while (r.next()) {
					tongtien = r.getInt("tongtien");
				}
				if (tongtien - sotien >= 50000) {
					tongtien = tongtien - sotien;
					String sql1 = "update khachhang set tongtien=" + tongtien
							+ " where sotaikhoan='" + stk + "'";
					PreparedStatement stmt1 = cn.prepareStatement(sql1);
					sodong = stmt1.executeUpdate();
					String sql2 = "insert into lichsugiaodich(sotaikhoan,ngaythang,sotien,lydo) Values('"
							+ stk + "'," + day + "," + sotien + ",'RutTien')";
					PreparedStatement stmt2 = cn.prepareStatement(sql2);
					stmt2.executeUpdate();
					System.out.println("Da ghi lai lich su giao dich");
				}
				cn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			out.println(sodong);
			out.flush();
			out.println(tongtien);
			out.flush();
		}

	}

	// Server duoc yeu cau doi mat khau
	public int Doimatkhau(String stk, String mkmoi) throws Exception {
		int sodong = 0;
		String url = "com.mysql.jdbc.Driver";
		synchronized (Getaccount(stk)) {

			try {
				Class.forName(url);
				Connection cn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/atm", "root", "");
				String sql = "update khachhang set matkhau='" + mkmoi
						+ "' where sotaikhoan='" + stk + "'";
				PreparedStatement stmt = cn.prepareStatement(sql);
				sodong = stmt.executeUpdate();// thuc thi
				cn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			out.println(sodong);
			out.flush();
			return sodong;
		}
	}

	// Server duoc yeu cau chuyen khoan
	public void Chuyenkhoan(String tkgui, String tknhan, int sotien)
			throws Exception {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		String day = sdf.format(cal.getTime());
		int stiennguoigui = 0;
		int stiennguoinhan = 0;
		int success = 0;
		String url = "com.mysql.jdbc.Driver";
		synchronized (Getaccount(tkgui)) {

			try {
				Class.forName(url);
				Connection cn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/atm", "root", "");
				String sql2 = "select* from khachhang where sotaikhoan='"
						+ tknhan + "'";
				PreparedStatement stmt1 = cn.prepareStatement(sql2);
				ResultSet rs2 = stmt1.executeQuery();
				while (rs2.next()) {
					stiennguoinhan = rs2.getInt("tongtien");
				}
				String sql = "select* from khachhang where sotaikhoan='"
						+ tkgui + "'";
				PreparedStatement stmt = cn.prepareStatement(sql);
				ResultSet rs = stmt.executeQuery();
				while (rs.next()) {
					stiennguoigui = rs.getInt("tongtien");
				}
				if (stiennguoigui - sotien >= 50000) {
					stiennguoigui = stiennguoigui - sotien;
					String sql3 = "update khachhang set tongtien="
							+ stiennguoigui + " where sotaikhoan='" + tkgui
							+ "'";
					PreparedStatement stmt3 = cn.prepareStatement(sql3);
					stmt3.executeUpdate();
					stiennguoinhan = stiennguoinhan + sotien;
					String sql4 = "update khachhang set tongtien="
							+ stiennguoinhan + " where sotaikhoan='" + tknhan
							+ "'";
					PreparedStatement stmt4 = cn.prepareStatement(sql4);
					stmt4.executeUpdate();
					// ghi lich su giao dich
					String sql5 = "insert into lichsugiaodich(sotaikhoan,ngaythang,sotien,lydo) Values('"
							+ tkgui
							+ "',"
							+ day
							+ ","
							+ sotien
							+ ",'ChuyenTien')";
					PreparedStatement stmt5 = cn.prepareStatement(sql5);
					stmt5.executeUpdate();
					String sql6 = "insert into lichsugiaodich(sotaikhoan,ngaythang,sotien,lydo) Values('"
							+ tknhan
							+ "',"
							+ day
							+ ","
							+ sotien
							+ ",'NhanChuyenTien')";
					PreparedStatement stmt6 = cn.prepareStatement(sql6);
					stmt6.executeUpdate();
					System.out.println("Da ghi lai lich su giao dich");
					success = 1;
				}

				cn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			out.println(success);
			out.flush();
			out.println(stiennguoigui);
			out.flush();
		}

	}

	// Server duoc yeu cau kiem tra tai khoan
	public void Kiemtra(String stk) throws Exception {
		int sodong = 0;
		String url = "com.mysql.jdbc.Driver";
		try {
			Class.forName(url);
			Connection cn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/atm", "root", "");
			String sql = "select* from khachhang where sotaikhoan='" + stk
					+ "'";
			PreparedStatement stmt1 = cn.prepareStatement(sql);
			ResultSet rs = stmt1.executeQuery();
			while (rs.next()) {
				int stiennguoinhan = rs.getInt("tongtien");
				sodong++;
			}
			cn.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		out.println(sodong);
		out.flush();
	}
}

public class Server {
	public static void main(String[] args) {		
		try {
			ServerSocket serversocket = new ServerSocket(8000);// tao socket
																// chap nhan ket
																// noi tu client
			int clientno = 1; // so may client
			while (true) {

				Socket connecttoclient = serversocket.accept();// chap nhan ket
				System.out.println("Khoi dong cho luong client so: "+clientno);
				// xac dinh dia chi
				InetAddress clientip = connecttoclient.getInetAddress(); // lay
																			// dia
																			// chi
																			// cua
																			// may client
				
				System.out.println("Host name cua client " + clientno + " la "
						+ clientip.getHostName());
				System.out.println("Dia chi IP cua client " + clientno + " la "
						+ clientip.getHostAddress()+"\n");
				SQLServer myobject = new SQLServer(connecttoclient);// ket noi
																	// den csdl
				Thread thserver = new Thread(myobject);// cho phep viec truy cap
														// csdl xay ra dong thoi
														// vs viec ket noi den
														// client
				thserver.start();
				clientno++; // tang so may client len 1

			}
		} catch (IOException e) {
			System.out.println("Server khong the ket noi....hay thu lai");
		}
	}
}
